import React, { Suspense, useEffect } from 'react';
import { useRoutes } from 'react-router'
import './global.css'

import routes from './routes'
import PageLoading from './component/pageLoading'

//获取session
import { getSession } from '../src/utils/axiosRequest/api'
//设备id
import { generateSessionParams } from "../src/utils/equipmentId/index"
//缓存
import { putCache } from '../src/utils/cache'
import { session_cache_key } from '../src/utils/cache/cacheKey'

function App() {

  const element = useRoutes(routes);

  useEffect(() => {
    //全局定时任务，刷新sesion
    const interval = setInterval(() => {
      //刷新
      const params = generateSessionParams()
      getSession(params)
        .then(response => {
          const data = response.data
          if (data.code === '000') {
            //保存session
            const data = response.data
            putCache(session_cache_key, data.data.sessionId);
          }
        })
        .catch(error => {

        });
    }, 15 * 60 * 1000); // 15分钟 = 900000毫秒
    return () => clearInterval(interval); // 组件卸载时清除定时器
  }, []);

  return (
    <Suspense fallback={<PageLoading />}>
      {element}
    </Suspense>
  );
}



export default App;
